
import javax.swing.*;


import java.awt.*;
import java.awt.event.*;

public class ListModifier extends JDialog{
	public static final int INCOME_LIST = 1;
	public static final int EXPENSE_LIST = 2;
	public static final int USER_LIST = 3;
	public static final int TRANS_METHOD_LIST = 4;
	private int listType;
	private MoneyKeeperModel model;
	private DefaultListModel listModel;
	private JList list;
	private JButton deleteItem, addItem, moveUpList, moveDownList, saveList, cancel, modifyItem;
	private String title;
	private JTextField submitArea;
	private MoneyKeeperView view;
	
	
	ListModifier(int listType, MoneyKeeperModel param_model,MoneyKeeperView dialogOwner){
		super(dialogOwner);
		this.view = dialogOwner;
		JRootPane rootPane = this.getRootPane();
		this.listType = listType; 
		this.getContentPane().setBackground(Color.WHITE);
		this.setModal(true);
		
		Icon boxIcon = new ImageIcon("icons\\Box_Blue.png");
		this.model = param_model;		
		this.getContentPane().setLayout(null);
		
		listModel = model.loadList(listType);
		
		if (listType == INCOME_LIST){
			title = "Income List";
		}else if (listType == EXPENSE_LIST){
			title = "Expense List";
		}else if (listType == USER_LIST){
			title = "User List";
		}else{
			title = "Transaction Method List";
		}
		
		this.setTitle(title + " Editor");
		JLabel listName = new JLabel(title,boxIcon, JLabel.LEFT);
		listName.setSize(300,35);
		listName.setFont(new Font("Calibri", Font.BOLD,20));
		
		listName.setLocation(10,1);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		deleteItem = new JButton("Delete");
		addItem = new JButton("Submit");
		moveUpList = new JButton("Up");
		moveDownList = new JButton("Down");
		saveList = new JButton("Save");
		cancel = new JButton("Cancel");
		list = new JList(listModel);
		modifyItem = new JButton("Modify");
		submitArea = new JTextField();
		
		
		
		addItem.setLocation(210,30);
		addItem.setSize(80,24);
		addItem.setBackground(Color.BLUE);
		addItem.setForeground(Color.WHITE);
		rootPane.setDefaultButton(addItem);
		
		submitArea.setLocation(10,30);
		submitArea.setSize(200,25);
		submitArea.setCursor(null);
		
		modifyItem.setSize(80,25);
		modifyItem.setLocation(210,60);
		
		moveUpList.setSize(80,25);
		moveUpList.setLocation(210,120);
		
		moveDownList.setSize(80,25);
		moveDownList.setLocation(210,150);
		
		deleteItem.setSize(80,25);
		deleteItem.setLocation(210,90);	
		
		
		saveList.setSize(80,25);
		saveList.setLocation(210,245);
		
		cancel.setSize(80,25);
		cancel.setLocation(210,275);
		
		addItem.addActionListener(new AddItemListener());
		deleteItem.addActionListener(new DeleteItemListener());
		moveUpList.addActionListener(new MoveUpListener());
		moveDownList.addActionListener(new MoveDownListener());
		modifyItem.addActionListener(new ModifyListListener());
		saveList.addActionListener(new SaveListListener());
		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				dispose();	
			}			
		});
		
		
	
		JScrollPane scroll = new JScrollPane(list);
		scroll.setSize(200,240);
		scroll.setLocation(10,60);
	
		
		this.getContentPane().add(listName);
		this.getContentPane().add(deleteItem);
		this.getContentPane().add(scroll);
		this.getContentPane().add(addItem);
		this.getContentPane().add(moveUpList);
		this.getContentPane().add(saveList);
		this.getContentPane().add(cancel);
		this.getContentPane().add(moveDownList);
		this.getContentPane().add(submitArea);
		this.getContentPane().add(modifyItem);
		
		this.setSize(310,340);
		this.setLocation(400,100);
		this.setResizable(false);
		this.setVisible(true);		
	}
	
	private class AddItemListener implements ActionListener{
		public void actionPerformed(ActionEvent ev) {
			String currValue = submitArea.getText();
			if (listModel.contains(currValue)){
				JOptionPane.showMessageDialog(null, "Item is already in the list.", "Input Rejected", 0);
			}else if (currValue.length() == 0){
				JOptionPane.showMessageDialog(null, "Cannot input empty value", "Input Rejected", 0);
			}else{
				listModel.addElement(currValue.toUpperCase());
				list.setSelectedIndex(list.getLastVisibleIndex());
				submitArea.setText("");
			}
			
		}		
	}
	private class DeleteItemListener implements ActionListener{
		public void actionPerformed(ActionEvent ev) {
			if(list.getSelectedValue() != null){
				listModel.remove(list.getSelectedIndex());
			}
		}		
	}
	
	private class MoveDownListener implements ActionListener{
		public void actionPerformed(ActionEvent ev) {
			String belowItem, currItem;
			int currIndex;
			//if current item is last one, then do nothing
			if(list.getSelectedValue() != null){
				currIndex = list.getSelectedIndex();
				currItem = (String) list.getSelectedValue();
				if(currIndex != list.getLastVisibleIndex()){
					belowItem = (String) listModel.get(currIndex + 1);
					listModel.set(currIndex+1, currItem);
					listModel.set(currIndex, belowItem);
					list.setSelectedIndex(currIndex+1);					
				}
			}		
		}		
	}
	
	private class MoveUpListener implements ActionListener{
		public void actionPerformed(ActionEvent ev) {
			String upperItem, currItem;
			int currIndex;
			//if current item is last one, then do nothing
			if(list.getSelectedValue() != null){
				currIndex = list.getSelectedIndex();
				currItem = (String) list.getSelectedValue();
				if(currIndex != 0){
					upperItem = (String) listModel.get(currIndex -1);
					listModel.set(currIndex-1, currItem);
					listModel.set(currIndex, upperItem);
					list.setSelectedIndex(currIndex-1);					
				}
			}
		}		
	}
	private class SaveListListener implements ActionListener{
		public void actionPerformed(ActionEvent ev) {
			model.saveList(listModel, listType);
			view.updateAllLists();
			dispose();
		}		
	}
	private class ModifyListListener implements ActionListener{
		public void actionPerformed(ActionEvent ev) {
			String s="";
			if (list.getSelectedValue() != null){
					s = JOptionPane.showInputDialog("Change "+ (String)(list.getSelectedValue()) +" to...", (String)(list.getSelectedValue()));
					if(s != null){					
						if(s.length()==0){
							JOptionPane.showMessageDialog(null, "Cannot input empty value.");				
						}else{
							listModel.set(list.getSelectedIndex(),s.toUpperCase());
						}
					}
			
			}else{
				JOptionPane.showMessageDialog(null, "Please Select the Item to modify");
			}
		}		
	}
	
}
